package com.ld.admin.controller.usercenter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.SystemUser;
import com.ld.admin.service.user.UserService;
import com.ld.shieldsb.annotation.duplicate.SubmitRequestIntercept;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "个人中心管理")
@ConditionalOnProperty(prefix = "modules.sc.usercenter", name = "myinfo.enable", havingValue = "true", matchIfMissing = true)
@Controller
@RequestMapping("${shieldsb.adminPath}/usercenter")
public class UserCenterController extends AdminBasicController<SystemUser> {
    @Autowired
    protected UserService userService;

    @ApiOperation(value = "查看我的信息", notes = "")
    @RequestMapping(value = "/myInfo", method = RequestMethod.GET)
    public String myInfoShow() {
        int type = getInt("t");
        setAttribute("t", type);
        SystemUser user = userService.getUser(Web.Util.getSessionUserId(request));
        request.setAttribute("user", user);
        return "a/usercenter/myInfo";
    }

    @ApiOperation(value = "跳转到修改我的信息页面", notes = "")
    @RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
    public String toUpdate() {
        int type = getInt("t");
        setAttribute("t", type);
        SystemUser user = userService.getUser(Web.Util.getSessionUserId(request));
        request.setAttribute("user", user);
        return "a/usercenter/update";
    }

    @ApiOperation(value = "修改我的信息", notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object myInfoUpdate(SystemUser user) {
        SystemUser sessionUser = (SystemUser) Web.Util.getSessionUser(request);
        user.setId(sessionUser.getId()); // 防止篡改用户
        Result result = userService.addOrUpdate(user, true);
        Web.Request.result(request, result); // 记录日志用，可以不填
        return result;
    }

    @ApiOperation(value = "修改我的密码", notes = "")
    @RequestMapping(value = "/pwdupdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object pwdUpdate(@RequestParam String password, @RequestParam String newPassword, @RequestParam String newPasswordConfirm) {
        SystemUser sessionUser = (SystemUser) Web.Util.getSessionUser(request);
        Result result = userService.updatePassword(sessionUser.getLoginCode(), password, newPassword, newPasswordConfirm);
        Web.Request.result(request, result); // 记录日志用
        return result;

    }

}
